package com.example.runningman.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.runningman.R;
import com.example.runningman.bean.Remark;
import com.example.runningman.friend.FriendActivity;
import com.example.runningman.util.EmojiUtil;
import com.example.runningman.util.SPUtil;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import okhttp3.OkHttpClient;

public class Remark_adapter extends RecyclerView.Adapter<Remark_adapter.ViewHolder> {
    List<Remark> mData;
    Activity mActivity;
    RequestOptions options;
    String useid;
    boolean f =false;
    public Remark_adapter(List<Remark> data, Activity activity) {
        this.mData = data;

        this.mActivity = activity;
        useid= SPUtil.getInstance(mActivity).getString("useid");
        options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .override(80,80)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;

        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_art, parent, false);



        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }
    Gson gson = new Gson();
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClick(v, position);
                }
                return false;
            }
        });
        holder.name.setText(mData.get(position).getNickname());
        try {
            holder.remark.setText(EmojiUtil.emojiRecovery(mData.get(position).getContent()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, FriendActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("userid",mData.get(position).getUserid());
                intent.putExtras(bundle);
                mActivity.startActivity(intent);
            }
        });

        if("".equals(mData.get(position).getReplyname())||mData.get(position).getReplyname()==null){

        }else {
            holder.textView.setText("回复");
            holder.rename.setText(mData.get(position).getReplyname());
            holder.rename.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, FriendActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("userid",mData.get(position).getReplyid());
                    intent.putExtras(bundle);
                    mActivity.startActivity(intent);
                }
            });
        }


    }



    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void refresh(List<Remark> data){
        this.mData = data;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView rename;
        TextView textView;
        TextView remark;
        public ViewHolder(View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.name);
            rename = itemView.findViewById(R.id.re_name);
            textView = itemView.findViewById(R.id.sss);
            remark = itemView.findViewById(R.id.art);


        }
    }
    final OkHttpClient client = new OkHttpClient();
    //私有属性
    private OnItemClickListener onItemClickListener = null;
    private OnItemLongClickListener onItemLongClickListener = null;
    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(View v,  int position);
    }
    public void setOnLongItemClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    //回调接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View v,  int position);
    }
}
